Macromedia Flex Article

Learning Flex Basics (Part 4): Working with Data Models


Table of Contents

  1. Introduction
  2. Create the Application Object and Application Layout
  3. Add the Form Controls and the DataGrid Controls
  4. Add the New Employee Model, Employee, and Location Models
  5. Instantiate the DataController Class, and Create the Employee and DataController Classes
  6. Add the DataController Methods
  7. Code Listing for data.mxml
  8. Code Listings for DataController.as and Employee.as
  9. Code Listings for locations.xml and employee.xml
  10. Where to Go From Here

Instantiate the DataController Class

To complete the data.mxml file, you instantiate the DataController class (which you build later). This is a straightforward procedure that is discussed in more detail in Learning Flex Basics (Part 2): Creating a Flex Calculator. You can instantiate an ActionScript class using a tag in an MXML file, but you must include a namespace reference for the location of the class so that the Flex compiler can locate it.

  1. Add an additional xmlns property to the Application tag; give the value of the namespace the value of "*" and do not assign a prefix to it:

    <mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*">
    
  2. After the Model tags, add a DataController tag with an id="dataController":

    <DataController id="dataController"/>
    	

    Note: You have already referenced this object in specifying handlers for the Button and DataGrids.

  3. Save the file. The data.mxml file is now complete.

Create the Employee Class

You now create the first of two external ActionScript classes required for the application. The Employee.as class is not used directly by data.mxml; instead it is imported into the DataController class you build next and used there as factory to generate new employee objects.

The class is simple, consisting of an empty constructor, properties for the employee name and location, and a method that assigns passed values to these properties.

The general structure of ActionScript classes is covered in Learning Flex Basics (Part 2): Creating a Flex Calculator.

  1. Open a new file in your editor and save it as Employee.as in the flex_tutorials directory.
  2. Add a class definition and empty constructor for the Employee class:

    class Employee 
    {	
    	// constructor	
    	public function Employee()	
    	{}
    }
    
  3. Above the constructor, add three undefined properties of type String for fName, lName, and location:

    public var fName:String;
    public var lName:String;
    public var location:String;
    
  4. After the constructor, add a public method called addEmpData that takes the parameters fName, lName, and location (all type String), and sets the properties of the same name equal to the passed parameters:

    public function addEmpData(fName:String,lName:String,location:String):Void
    {	
    	this.fName=fName;	
    	this.lName=lName;	
    	this.location=location;
    }
    
  5. Save the file.

Create the DataController Class

Next you create the DataController class, for which you created the instantiation in data.mxml earlier. This class contains methods used to handle events in data.mxml. This class will import two addtional classes:

  • Employee: The ActionScript class you just created
  • mx.managers.DragManager: A static class from the Flex class library that contains properties and methods used in handling drag-and-drop event

You will use only a single property of the DragManager class here, but it has much more functionality that can help you implement business rules around drag and drop operations. Consult the Flex documentation for more details.

  1. Open a new file in your editor and save it as DataController.as in the flex_tutorials directory.
  2. At the top of the file, add import statements to import the Employee and mx.managers.DragManager classes:

    import mx.managers.DragManager;
    import Employee;
    
  3. After the import statements, add a class definition with an empty constructor for the DataController class:

    class DataController
    {
    	// constructor
    	public function DataController()	
    	{}
    }